//
//  MovieDetailViewController.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"
#import "AppTools.h"
#import "LoginViewController.h"
#import "DataBaseSingleton.h"
#import "MBProgressHUD.h"
#import "MovieCollectViewController.h"

@interface MovieDetailViewController ()

@property (nonatomic, retain)MovieDetailView *movieDV;
@property (nonatomic, retain)UIScrollView *scrollV;
@property (nonatomic, retain)MBProgressHUD *hud;

@end

@implementation MovieDetailViewController

- (void)dealloc
{
    [_movieL release];
    [_movieD release];
    [_scrollV release];
    [_movieDV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.movieL.movieName;
    
#pragma mark -- 用UIScrollView详情
    self.scrollV = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.scrollV];
    [_scrollV release];
#pragma mark -- 左按钮
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)] autorelease];
#pragma mark -- 右按钮
    UIImage *rightimage = [UIImage imageNamed:@"btn_nav_share.png"];
    rightimage = [rightimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:rightimage style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)] autorelease];
#pragma mark -- view加到scrollView上
    self.movieDV = [[MovieDetailView alloc] init];
    [self.scrollV addSubview:self.movieDV];
    [_movieDV release];
#pragma mark -- 请求数据
    [self getAFNetWorkingData];

}


#warning 请求数据,并将数据存入model中
- (void)getAFNetWorkingData{
    
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@", self.movieL.movieId];
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        NSMutableDictionary *dic = result;
        self.movieD = [[MovieDetail alloc] init];
        [self.movieD setValuesForKeysWithDictionary:[dic objectForKey:@"result"]];
        self.movieD.picture = self.movieL.pic_url;
        self.movieD.title = self.movieL.movieName;
        [_movieD release];
        self.movieDV.movieDDV = self.movieD;
        [self.movieDV sendInfo];
#pragma mark -- scrollView的核心属性(自适应高度)
        CGFloat height = self.movieDV.plotDetailLabel.frame.origin.y + self.movieDV.plotDetailLabel.frame.size.height + 70;
        self.scrollV.contentSize = CGSizeMake(0, height);
    }];
    
}

// 左
- (void)leftBarButtonAction:(UIBarButtonItem *)button{
    [self.navigationController popViewControllerAnimated:YES];
}
// 右
- (void)rightBarButtonAction:(UIBarButtonItem *)button{
    NSString *name = [NSUSER objectForKey:@"name"];
    NSString *isLogin = [NSUSER objectForKey:@"isLogin"];
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    if (![@"1" isEqualToString:isLogin]) {
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naVC animated:YES completion:^{
            
            
        }];
        [loginVC release];
    } else {
        NSMutableArray *array = [DATABASE selectCollectUserName:name];
        BOOL result = [array containsObject:self.movieL.movieName];
        if (result) {
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"已经收藏过啦";
            self.hud.mode = MBProgressHUDModeText;
            //        self.hud.delegate = self;
            [self.hud hide:YES afterDelay:1];
        } else {
            [DATABASE inserUserName:name Title:self.movieL.movieName];
            MovieCollectViewController *mCVC = [[MovieCollectViewController alloc] init];
            mCVC.nameTitle = self.movieL.movieName;
            [mCVC release];
            
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"收藏成功";
            self.hud.mode = MBProgressHUDModeText;
            //        self.hud.delegate = self;
            [self.hud hide:YES afterDelay:1];
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
